layui.use(['form', 'layer', 'table', 'laydate', 'func', 'ax', 'tableSelect'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var func = layui.func;
    var $ax = layui.ax;
    var tableSelect = layui.tableSelect;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    /**
     * 初始化表格
     */
    var Gift = {
        tableId: "GiftTable" //表格id
    };

    /**
     * 初始化表格的列
     */
    Gift.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'name', align: "center", sort: true, title: '礼品名称'},
            {field: 'giftLevelName', align: "center", sort: true, title: '礼品级别'},
            {field: 'giftTypeName', align: "center", sort: true, title: '礼品分类'},
            {field: 'giftNo', align: "center", sort: true, title: '礼品编号'},
            {field: 'img', align: "center", sort: true, title: '礼品图片',
                templet: function(object){
                    return "<img src='\gift_img\>" + object.img + "'</img>";
                }
            },
            {field: 'giftNum', align: "center", sort: true, title: '礼品数量',
                templet: function(object){
                    return "<span style='color:#FF5722'>" + object.giftNum + "</span>";
                }
            },
            {field: 'brand', align: "center", sort: true, title: '品牌',
                templet: function(object){
                    if(object.brand == '' || object.brand == null || object.brand == undefined){
                        return '-';
                    }else {
                        return object.brand;
                    }
                }
            },
            {field: 'source', align: "center", sort: true, title: '来源',
                templet: function(object){
                    if(object.source == '' || object.source == null || object.source == undefined){
                        return '-';
                    }else {
                        return object.source;
                    }
                }
            },
            {field: 'remark', align: "center", sort: true, title: '备注',
                templet: function(object){
                    if(object.remark == '' || object.remark == null || object.remark == undefined){
                        return '-';
                    }else {
                        return object.remark;
                    }
                }
            },
            {field: 'updateDate', align: "center", sort: true, title: '更新时间'},
            {field: 'userName', align: "center", sort: true, title: '更新者'},
            {align: 'center', toolbar: '#tableBar', title: "操作", minWidth: 80}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Gift.tableId,
        url: Feng.ctxPath + '/web/gift/list',
        page: true,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        height: "full-98",
        cellMinWidth: 100,
        cols: Gift.initColumn()
    });

    /**
     * 点击查询按钮
     */
    Gift.search = function () {
        var queryData = {};
        queryData['giftTypeName'] = $("#giftTypeName").val();
        queryData['giftLevelName'] = $("#giftLevelName").val();
        table.reload(Gift.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击重置按钮
     */
    Gift.reset = function () {
        $("#giftTypeName").val("");
        $("#giftLevelName").val("");

        form.render();

        var queryData = {};
        queryData['giftTypeName'] = "";
        queryData['giftLevelName'] = "";
        Gift.search();
    };

    /**
     * 弹出添加对话框
     */
    Gift.openAdd = function () {
        func.open({
            title: "添加礼品",
            maxmin: true,
            height: 700,
            content: Feng.ctxPath + '/web/gift/addOrEdit',
            tableId: Gift.tableId
        });
    };

    /**
     * 弹出编辑对话框
     */
    Gift.onEdit = function (data) {
        func.open({
            title: "编辑礼品(" + data.name + ")",
            maxmin: true,
            height: 700,
            content: Feng.ctxPath + '/web/gift/addOrEdit?giftId=' + data.id,
            tableId: Gift.tableId
        });
    };

    /**
     * 点击删除按钮
     *
     * @param data 点击按钮时候的行数据
     */
    Gift.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/web/gift/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Gift.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("magicBoxId", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除该礼品(" + data.name + ")?", operation);
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Gift.search();
    });

    // 重置按钮点击事件
    $('#btnReset').click(function () {
        Gift.reset();
    });

    // 添加按钮点击事件
    $("#btnAdd").click(function(){
        Gift.openAdd();
    });

    //下拉选择
    //默认值:只需要在触发input上添加 ts-selected="1,2,3" 属性即可 值需与checkedKey对应
    tableSelect.render({
        elem: '#giftTypeName',	//定义输入框input对象 必填
        checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'name',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '礼品分类搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/web/giftType/list',
            where: {'name': ''}, //附加参数
            cols: [
                [
                    {type: 'radio'},
                    {field: 'name', title: '名称', align: 'center'},
                    {field: 'remark', align: "center", sort: true, title: '备注',
                        templet: function(object){
                            if(object.remark == '' || object.remark == null || object.remark == undefined){
                                return '-';
                            }else {
                                return object.remark;
                            }
                        }
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            $("#giftTypeName").val(data.data[0].name);
        }
    });

    //下拉选择
    //默认值:只需要在触发input上添加 ts-selected="1,2,3" 属性即可 值需与checkedKey对应
    tableSelect.render({
        elem: '#giftLevelName',	//定义输入框input对象 必填
        checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'name',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '礼品级别搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/web/giftLevel/list',
            where: {'name': ''}, //附加参数
            cols: [
                [
                    {type: 'radio'},
                    {field: 'name', align: "center", sort: true, title: '名称'},
                    {field: 'remark', align: "center", sort: true, title: '备注',
                        templet: function(object){
                            if(object.remark == '' || object.remark == null || object.remark == undefined){
                                return '-';
                            }else {
                                return object.remark;
                            }
                        }
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            $("#giftLevelName").val(data.data[0].name);
        }
    });

    // 工具条点击事件
    table.on('tool(' + Gift.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Gift.onEdit(data);
        } else if(layEvent === 'delete'){
            Gift.onDelete(data);
        }
    });

});

$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});